<?php
namespace app\admin\controller;

use app\think\Controller;
use app\admin\model\Paper as PaperModel ;
use \think\Validate;
use \think\Config as Config;
/**
 * 
 * 试卷
 *
 * @name Examination
 * @author Aninet Sat Jun  9 19:58:46 2018
 */
class Paper extends Base{

    /**
     * 试卷的列表页面 
     * 
     * @return 
     * @date Sat Jun  9 19:57:13 2018
     */
    public function index(){
        if($this->req->has('page')){
            $page = $this->req->get('page');
            if($page == 1){
                $page = 0;
            }else{
                $page = $page-1;
            }
            $rows = Config::get('paginate.list_rows');
            $rows = 5;
            $model = new PaperModel;
            // $data = $model->field(['pap_id','pap_name','pap_type','pap_mark','pap_createtime'])->limit($page*$rows,$rows)->select();
            $data = $model->field(['pap_id','pap_name','pap_type','pap_mark','pap_createtime'])->paginate($rows,false, ['var_page' => 'page']);
            $this->assign('data',$data);
            // $this->assign('list',$list);
        } else {
            
            $rows = Config::get('paginate.list_rows');
            $rows = 5;

            $model = new PaperModel;
            // $data = $model->field(['pap_id','pap_name','pap_type','pap_mark','pap_createtime'])->limit(0,$rows)->select();
            $data = $model->field(['pap_id','pap_name','pap_type','pap_mark','pap_createtime'])->paginate($rows);
            $this->assign('data',$data);
            // $this->assign('list',$list);
        }

        echo $this->fetch();
    }

    /**
     * 创建试卷的页面；
     * 
     * @return html
     * @date Sun Jun 10 18:28:53 2018
     */
    public function add(){
        return $this->fetch();
    }
    public function create(){
        return $this->fetch();
    }
    /**
     * 插入试卷数据到数据库
     * 
     * @return success/error
     * @date Sun Jun 10 18:59:51 2018
     */
    public function insert(){
        $post = $this->req->post();
        $rule = [
            'name' => 'require',
            'type' => 'require'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $data['pap_name'] = $post['name'];
            $data['pap_type'] = $post['type'];
            $data['pap_mark'] = $post['mark'];
            $data['pap_createtime'] = time();
            $data['pap_time'] = $post['time'];
            $paperModel = new \app\admin\model\Paper;
            $result = $paperModel->save($data);
            if($result){
                $info['msg'] = '添加成功';
                echo $this->right($info);
            }else{
                $info['msg'] = '添加失败';
                echo $this->wrong($info);

            }
        } else {
            $info['msg'] = '添加失败';
            echo $this->wrong($info);
        }
    }

    /**
     * 编辑试卷页面
     * 
     * @return 
     * @date Sun Jun 10 21:03:16 2018
     */
    public function edit(){
        $id = $this->req->param('id');
        
        $paperModel = new \app\admin\model\Paper;
        $result = $paperModel->field(['pap_id','pap_name','pap_type','pap_mark','pap_createtime'])->where(['pap_id'=>$id])->find();
        if($result){
            $this->assign('result',$result);
            return $this->fetch();
        }else{
            return $this->error('获取数据失败');
        }
        
    }

    /**
     * 更新试卷的信息。（不是试题）；
     * 
     * @return success/error html
     * @date Sun Jun 10 21:24:23 2018
     */
    public function update()
    {
        $post = $this->req->post();
        $rule = [
            'name' => 'require',
            'type' => 'require',
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            
            $id = $post['id'];
            $data['pap_name'] = $post['name'];
            $data['pap_type'] = $post['type'];
            $data['pap_mark'] = $post['mark'];
            $data['pap_createtime'] = time();
            $data['pap_time'] = $post['time'];
            $paperModel = new \app\admin\model\Paper;
            $result = $paperModel->where(['pap_id'=>$id])->update($data);
            if($result){
                $info = [
                    'msg' => '更新成功',
                ];
                echo $this->right($info);
            }else{
                $info = [
                    'msg' => '更新失败',
                ];
                echo $this->wrong($info);
            }
        } else {
            echo $this->wrong(['msg'=>'需要填写的数据不全']);
        }
        return;
    }

    /**
     * 显示某个试卷的试题(选择题)；
     * 
     * @return html
     * @date Sun Jun 10 21:29:47 2018
     */
    public function choice()
    {
        $get = $this->req->get();
        $id = $get['id'];
        $paperModel = new \app\admin\model\Paper;
        $data = $paperModel->where(['pap_id'=>$id])->select();
        if($data){
            $this->assign('data',$data);
            $this->assign('id',$id);
            return $this->fetch();
        }else{
            return $this->error('没有数据');
        }

        
    }

    /**
     * 删除一张试卷
     * 
     * @return 
     * @date Fri Jul 13 21:35:28 2018
     */
    public function remove(){
        if($this->req->has('id')){
            $id = $this->req->get('id');
            $model = new \app\admin\model\Paper;
            // $result = $model->destroy(['pap_id'=>$id]);
            $result = 0;
            if($result){
                echo $this->wrong(['msg'=>'删除失败']);
            } else {
                echo $this->right(['msg'=>'删除成功']);
            }
        }else{
            return;
        }
    }

    public function search(){
        $get = $this->req->get();
        // var_dump($get);exit();
        $rules = ['keyword'=>'require'];
        $validate = new Validate($rules);
        $result = $validate->check($get);
        if($result){
            if(isset($get['page'])){
                $page = $get['page'];
            } else {
                $page = 0;
            }
            $rows = Config::get('paginate.list_rows');
            $rows = 5;
            $model = new PaperModel;
            // $data = $model->field(['pap_id','pap_name','pap_type','pap_mark','pap_createtime'])
            //       ->where("`pap_name` LIKE '%".$get['keyword']."%'")
            //       ->limit($page*$rows,$rows)->select();
            // var_dump($model->getLastSql());exit();
            $data = $model->where("`pap_name` LIKE '%".$get['keyword']."%'")->paginate(2,false, ['var_page' => 'page','query'=>['keyword'=>$get['keyword']]]);
            
            $this->assign('data',$data);
            // $this->assign('list',$list);
            return $this->fetch();
        }else{
            return $this->error('你没有输入关键词');
        }

    }
}

